<?php 
include_once 'viper/autoloader.php';

class viper_dao
{
	private static $_connection;
	private static $_selection;
	
	private static function _open_db(){
		$dbparam = include '../dbparam.php';
		
		self::$_connection = mysql_connect($dbparam['DBHOST'], $dbparam['DBUSER'], $dbparam['DBPASS']);
		if(!self::$_connection) die ('Could not connect to Database' . mysql_error());
		
		self::$_selection = mysql_select_db($dbparam['VIPERDB'], self::$_connection);
		if(!self::$_selection) die ('Could not select Database' . mysql_error());
	}
	
	private static function _close_db(){
		mysql_close(self::$_connection);
	}
	
	
	public static function query($sql){
		self::_open_db();
		
		$result = mysql_query($sql);
		if(!$result) die ('Could not query: ' . $sql . ' becasue ' . mysql_error());
		
		if($result === true) return true;
		
		$data = array();
		
		while($row = mysql_fetch_assoc($result)){
			$data[] = $row;
		}
		
		self::_close_db();
		
		return $data;
	}	
	
	// *Parameters for getParameter and setParameter*
	// feature_number
	// user_number
	// max_score
	// min_score
	// friends_number
	// cluster_number
	
	public static function getParameter($param){		
		$sql = 'SELECT value
		        FROM parameters
		        WHERE parameter = "' . $param . '"';
		
		$res = self::query($sql);
		if(!res) return -1;
		
		return $res[0][0];
	}
	
	public static function setParameter($param, $value){
		$sql = 'UPDATE parameters
		        SET value = "' . $value . '"
		        WHERE parameter = "' . $param . '"';
		
		$res = self::query($sql);
		return $res;
	}
}

?>